home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 20 code / Pict Tricks / Batch CLUTLess / App.c next >
Encoding:
Text File  |  1994-10-14  |  8.9 KB  |  395 lines  |  [TEXT/MMCC]

  1. //---------------------------------------------------------------------
  2. //---------------------------------------------------------------------
  3. //
  4. //    Horrible Rickety Shell, by Dave Johnson
  5. //
  6. //    © Copyright 1985 - 1994 Anyone Who Wants It,
  7. //    All Rights Energetically Heaved as far away from me as possible.
  8. //    Use at your own (considerable) risk.
  9.  
  10.  
  11. #include "AppInterface.h"
  12. #include <GestaltEqu.h>
  13.  
  14. /* The two shell globals we need */
  15. extern MenuHandle    gShellMenuHandles[];
  16. extern Boolean        gDoneFlag;
  17.  
  18. ////////////////
  19. // Constants
  20.  
  21. #define kBatchMenuID        131
  22.     #define    iBatch            1
  23.  
  24. ////////////////
  25. // Globals
  26.  
  27. WindowRecord        gWStore;
  28. WindowPtr             gWindow = nil;
  29. MenuHandle            gBatchMenuHndl = nil;
  30.  
  31. // Protos for SelectFolder
  32. void InitSelectFolder(void);
  33.  
  34.  
  35. // An Apple Event support routine
  36. OSErr    MyGotRequiredParams (AppleEvent *theAEvent);
  37.  
  38. /*  A chance to pre-handle an event */
  39. Boolean AppDoEvent(EventRecord *event)
  40. {
  41.     Boolean        handled = false;
  42.     
  43.     // Do Stuff
  44.     return handled;
  45. }
  46.  
  47. /* Called by the Shell at startup time */
  48. Boolean AppInit(void)     /* returns false if initialization fails */
  49. {
  50.     Rect        tempRect = {0, 0, 300, 300};
  51.     long        gestaltResponse;
  52.     OSErr        err;
  53.     
  54.     // Check for Apple Event Manager
  55.     err = Gestalt(gestaltAppleEventsAttr, &gestaltResponse);
  56.     if( err != noErr || !(gestaltResponse & (1L >> gestaltAppleEventsPresent)) )
  57.         return false;
  58.     
  59.      // Get the menu, draw menu bar
  60.     gBatchMenuHndl = GetMenu(kBatchMenuID);
  61.     if(gBatchMenuHndl == nil)
  62.         return false;
  63.     (*gBatchMenuHndl)->menuID = kBatchMenuID;
  64.     InsertMenu(gBatchMenuHndl, 0);
  65.  
  66.     DrawMenuBar();
  67.  
  68.     // Install the Apple Event Handlers
  69.     err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, &AppOAPPHandler, 0, FALSE);
  70.     if(err != noErr)
  71.         return false;
  72.      err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, &AppODOCHandler, 0, FALSE);
  73.     if(err != noErr)
  74.         return false;
  75.     err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, &AppPDOCHandler, 0, FALSE);
  76.     if(err != noErr)
  77.         return false;
  78.     err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, &AppQUITHandler, 0, FALSE);
  79.     if(err != noErr)
  80.         return false;
  81.     
  82.     // Initialize the select folder stuff
  83.     InitSelectFolder();
  84.  
  85.     // Build a window, invisible
  86.     OffsetRect(&tempRect, 20, GetMBarHeight() + 30);
  87.     gWindow = NewCWindow(&gWStore, &tempRect, "\pA Window", false,
  88.                         documentProc, (WindowPtr)(-1), true, nil);
  89.     
  90.     return (gWindow != nil);
  91. }
  92.  
  93. /* Called when the shell receives an Activate event. */
  94. void AppActivate(WindowPtr wind, Boolean activate)
  95. {
  96. }
  97.  
  98. /* Called when a window needs updating. BeginUpdate() has already been called, and the 
  99. port is set to the appropriate window */
  100. void AppUpdate(EventRecord *event)
  101. {
  102.     if((WindowPtr) event->message == gWindow)
  103.     {
  104.         EraseRect(&gWindow->portRect);
  105.     }
  106. }
  107.  
  108. /* Called when the shell recieves a null event. */
  109. void AppIdle(EventRecord *Event)
  110. {
  111. }
  112.  
  113. /* Called when there is a click in the content of a window. The port is already set to 
  114. the window, and thePt is in local coords. */
  115. void AppClick(Point thePt, WindowPtr whichWindptr, Boolean doubleClick)
  116. {
  117. }
  118.  
  119. /* Called when there is a click in the grow region. */
  120. void AppGrowWindow(WindowPtr wind, Point where, Rect *desk)
  121. {
  122. }
  123.  
  124. /* Called when the user clicks in the zoom box of a window. */
  125. void     AppZoomWindow(WindowPtr wind, short zoomDir)
  126. {
  127. }
  128.     
  129. /* Called when there is a click in the menu bar, before the menu is shown. This is
  130. the app's opportunity to enable and disable menu items. */
  131. void    AppAdjustMenus()
  132. {
  133. }    
  134.  
  135. /* called when a menu other than Apple, File, or Edit is used. */
  136. void AppMenu(short id, short item)
  137. {
  138.     // Prototype
  139.     OSErr DoBatchClutStrip(void);
  140.  
  141.     // Batch menu
  142.     if(id == kBatchMenuID)
  143.     {
  144.         OSErr    err;
  145.         
  146.         // Only one item in menu
  147.         SetPort(gWindow);
  148.         err = DoBatchClutStrip();
  149.         if(err != noErr)
  150.             DebugStr("\pError in DoBatchClutStrip");
  151.     }
  152. }
  153.  
  154. /* Called when the user selects "New" from the File menu. */            
  155. void    AppNew(void)
  156. {
  157. }
  158.  
  159. /* Called when the user selects "Open" from the File menu. */
  160. void    AppOpen(void)
  161. {
  162. }
  163.  
  164.  
  165. /* Called when the user selects "Close" from the File menu or clicks the close box 
  166. of a window. */            
  167. Boolean    AppClose(void)
  168. {
  169.     /* returns false if the user cancels the save */
  170.     
  171.     return true;
  172. }
  173.  
  174. /* Called when the user selects "Save" from the File menu. */
  175. Boolean    AppSave(void)
  176. {
  177.     /* returns false if the user cancels the save */
  178.     return true;
  179. }
  180.  
  181. /* Called when the user selects "Save As..." from the File menu. */
  182. Boolean    AppSaveAs(void)
  183. {
  184.     /* returns false if the user cancels the save */
  185.     return true;
  186. }
  187.  
  188. /* Called when the user selects "Page Setup..." from the File menu. */
  189. void AppPageSetup(void)
  190. {
  191. }
  192.  
  193. /* Called when the user selects "Print..." from the File menu. */
  194. void AppPrint(void)
  195. {
  196. }
  197.  
  198. /* Called when the user selects "Undo" from the Edit menu. */
  199. void AppUndo(void)
  200. {
  201. }
  202.  
  203. /* Called when the user selects "Cut" from the Edit menu. */
  204. void AppCut(void)
  205. {
  206. }
  207.  
  208. /* Called when the user selects "Copy" from the Edit menu. */
  209. OSErr AppCopy(void)
  210. {
  211.     return noErr;
  212. }            
  213.             
  214. /* Called when the user selects "Paste" from the Edit menu. */
  215. void AppPaste(void)
  216. {
  217. }
  218.  
  219. /* Called when the user selects "Clear" from the Edit menu. */
  220. void AppClear(void)
  221. {
  222. }
  223.  
  224. /*     Called when the user chooses "Quit" from the File menu. If the user cancels
  225. the save it returns false, otherwise it returns true and the shell quits */
  226. Boolean AppQuit(void)
  227. {
  228.     /* returns false if the user cancels the save at any point */
  229.     return true;
  230. }
  231.  
  232. /* Called when the shell is about to quit, just deallocates memory. */
  233. void AppCleanUp(void)
  234. {
  235.     if(gWindow != nil)
  236.     {
  237.         CloseWindow(gWindow);
  238.         gWindow = nil;
  239.     }
  240. }
  241.  
  242. /* Apple Event handlers */
  243. pascal OSErr AppOAPPHandler(AppleEvent *theAEvent, AppleEvent *reply, long refcon)
  244. {
  245.     OSErr    err;
  246.  
  247.     err = MyGotRequiredParams(theAEvent);
  248.     if (err)
  249.         return err;
  250.     else {
  251.         // Just do an AppNew()
  252.         AppNew();
  253.         return noErr;
  254.     }
  255. }
  256.  
  257. pascal OSErr AppODOCHandler(AppleEvent *theAEvent, AppleEvent *reply, long refcon)
  258. {
  259.     FSSpec        myFSS;
  260.     AEDescList    docList;
  261.     OSErr        err;
  262.     long        index, itemsInList;
  263.     Size        actualSize;
  264.     AEKeyword    keywd;
  265.     DescType    returnedType;
  266.  
  267.     // get the direct parameter--a descriptor list--and put
  268.     // it into docList
  269.     err = AEGetParamDesc(theAEvent, keyDirectObject,
  270.                             typeAEList, &docList);
  271.     if (err)
  272.         return err;
  273.  
  274.     // check for missing required parameters
  275.     err = MyGotRequiredParams(theAEvent);
  276.     if (err) {
  277.         // an error occurred:  do the necessary error handling
  278.         err = AEDisposeDesc(&docList);
  279.         return    err;
  280.     }
  281.  
  282.     // count the number of descriptor records in the list
  283.     err = AECountItems (&docList, &itemsInList);
  284.  
  285.     // now get each descriptor record from the list, coerce
  286.     // the returned data to an FSSpec record, and open the
  287.     // associated file
  288.     for (index = 1; index <= itemsInList; index++)
  289.     {
  290.         err = AEGetNthPtr(&docList, index, typeFSS, &keywd,
  291.                             &returnedType, (Ptr)&myFSS,
  292.                             sizeof(myFSS), &actualSize);
  293.         if (err)
  294.             SysBeep(10);
  295.         
  296.         // Open the file
  297.         SysBeep(10); // Sorry, no files to open yet
  298.         
  299.         if (err)
  300.             SysBeep(10);
  301.     }
  302.  
  303.     err = AEDisposeDesc(&docList);
  304.     return    noErr;
  305. }
  306.  
  307. pascal OSErr AppPDOCHandler(AppleEvent *theAEvent, AppleEvent *reply, long refcon)
  308. {
  309.     FSSpec        myFSS;
  310.     AEDescList    docList;
  311.     OSErr        err;
  312.     long        index, itemsInList;
  313.     Size        actualSize;
  314.     AEKeyword    keywd;
  315.     DescType    returnedType;
  316.  
  317.     // get the direct parameter--a descriptor list--and put
  318.     // it into docList
  319.     err = AEGetParamDesc(theAEvent, keyDirectObject,
  320.                             typeAEList, &docList);
  321.     if (err)
  322.         return err;
  323.  
  324.     // check for missing required parameters
  325.     err = MyGotRequiredParams(theAEvent);
  326.     if (err) {
  327.         // an error occurred:  do the necessary error handling
  328.         err = AEDisposeDesc(&docList);
  329.         return    err;
  330.     }
  331.  
  332.     // count the number of descriptor records in the list
  333.     err = AECountItems (&docList, &itemsInList);
  334.  
  335.     // now get each descriptor record from the list, coerce
  336.     // the returned data to an FSSpec record, and open the
  337.     // associated file
  338.     for (index = 1; index <= itemsInList; index++)
  339.     {
  340.         err = AEGetNthPtr(&docList, index, typeFSS, &keywd,
  341.                             &returnedType, (Ptr)&myFSS,
  342.                             sizeof(myFSS), &actualSize);
  343.         if (err)
  344.             SysBeep(10);
  345.         
  346.         // Print the file
  347.         SysBeep(10);
  348.         
  349.         if (err)
  350.             SysBeep(10);
  351.     }
  352.  
  353.     err = AEDisposeDesc(&docList);
  354.     return    noErr;
  355. }
  356.  
  357. pascal OSErr AppQUITHandler(AppleEvent *theAEvent, AppleEvent *reply, long refcon)
  358. {
  359.     OSErr    err;
  360.  
  361.     // check for missing required parameters
  362.     err = MyGotRequiredParams(theAEvent);
  363.     if (err) {
  364.         // an error occurred:  do the necessary error handling
  365.         return    err;
  366.     }
  367.     
  368.     // Set the global flag if the user doesn't cancel
  369.     gDoneFlag = AppQuit();
  370.     if(gDoneFlag == false)
  371.         return    userCanceledErr;
  372.     else
  373.         return    noErr;
  374. }
  375.  
  376. OSErr    MyGotRequiredParams (AppleEvent *theAEvent)
  377. {
  378.     DescType    returnedType;
  379.     Size        actualSize;
  380.     OSErr        err;
  381.  
  382.     err = AEGetAttributePtr(theAEvent, keyMissedKeywordAttr,
  383.                     typeWildCard, &returnedType,
  384.                     nil, 0, &actualSize);
  385.  
  386.     if (err == errAEDescNotFound)    // you got 'em all
  387.         return    noErr;
  388.     else
  389.         if (err == noErr)  // you missed a required parameter
  390.             return    errAEParamMissed;
  391.         else    // the call to AEGetAttributePtr failed
  392.             return    err;
  393. }
  394.  
  395.